package com.cbd.dao.impl;

import java.util.List;


import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.cbd.dao.PurposeDao;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.Purpose;
@Component
@Repository
public class PurposeDaoImpl implements PurposeDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public List<Purpose> findAllPurposeByInfoId(int infoId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateIsDeal(int PopurseId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addPurpose(int infoId, int renterId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public PageBean findPurposeByInfoId(PageBean page, int infoId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query  = session.createQuery("from Purpose p where p.rentInfo.rentInfoID = ?");
		query.setInteger(0, infoId);
		int num = (page.getCurPage()-1)*page.getPageSize();
		query.setFirstResult(num);
		query.setMaxResults(page.getPageSize());
		List<Purpose> list = query.list();
		for (Purpose p : list) {
			p.setRecord(null);
			p.getRenter().setPurposes(null);
			p.getRenter().setUser(null);
			p.getRentInfo().setPurposes(null);
			p.getRentInfo().getPersonalcarport().setLandlady(null);
		}
		page.setList(list);
		return page;
	}

	@Override
	public int findPurposeNumById(int infoID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Purpose p where p.rentInfo.rentInfoID = ?");
		query.setInteger(0, infoID);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public void updateIsDealById(int purposeID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Purpose p set p.isDeal = true where p.purposeID = ?");
		query.setInteger(0, purposeID);
		System.out.println("1111");
		query.executeUpdate();
	}

	@Override
	public void delPurposeById(int purposeID,int rentInfoID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("delete from Purpose p where p.purposeID <> ? and p.rentInfo.rentInfoID = ?");
		query.setInteger(0, purposeID);
		query.setInteger(1, rentInfoID);
		query.executeUpdate();
	}

	@Override
	public Purpose findPurposeById(int purposeID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Purpose p = (Purpose) session.get(Purpose.class, purposeID);
		return p;
	}

	@Override
	public int checkHasPurpose(int rentInfoID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Purpose p where p.rentInfo.rentInfoID = ?");
		query.setInteger(0, rentInfoID);
		Long l  = (Long) query.uniqueResult();
		System.out.println("dao层"+l);
		return l.intValue();
	}

	@Override
	public int addPurpose(Purpose purpose) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		int result = (int) session.save(purpose);
		return result;
	}

}
